using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using InfoExpress.HIV.WinApp.Popups;
using InfoExpress.HIV.WinApp.Forms;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.HIV.WinApp.Business;

namespace InfoExpress.HIV.WinApp
{
  public partial class FrmMain : XtraForm
  {
    #region Members and Properties

    /// <summary>
    /// Set status text of status bar.
    /// </summary>
    /// <param name="status"></param>
    public string StatusText
    {
      get { return bbiStatus.Caption; }
      set { bbiStatus.Caption = value; }
    }

    /// <summary>
    /// Application work status
    /// </summary>
    public ApplicationStatus Status { get; set; }

    #endregion

    #region Constructors

    public FrmMain()
    {
      InitializeComponent();
      MainFormUtils.InitializeUtils(this);
    }

    #endregion

    #region Public Methods
    /// <summary>
    /// Start progress bar of status bar.
    /// </summary>
    public void StartProgress()
    {
      ((RepositoryItemMarqueeProgressBar)beiProgress.Edit).Stopped = false;
    }

    /// <summary>
    /// Stop progress bar of status bar.
    /// </summary>
    public void StopProgress()
    {
      ((RepositoryItemMarqueeProgressBar)beiProgress.Edit).Stopped = true;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="neededForm"></param>
    public XtraForm FindForm(Type neededForm)
    {
      foreach (Form child in MdiChildren)
      {
        if (child.GetType() == neededForm)
          return child as XtraForm;
      }
      return null;
    }
    #endregion

    #region Helper Methods

    private void Login()
    {
      using (FrmLogin frmLogin = new FrmLogin { Owner = this })
      {
        if (frmLogin.ShowDialog() == DialogResult.OK)
        {
          #region Permission
          ApplyMenuPermission();
          #endregion

          #region Title
          Text = MainFormUtils.GetTitle();
          #endregion
        }
        else
        {
          bbiThoat.PerformClick();
        }
      }
    }

    /// <summary>
    /// Display a child form. If it is exists, it is activated.
    /// </summary>
    /// <param name="frmIsLoaded"></param>
    private void LoadForm(Type loadedForm)
    {
      foreach (Form child in MdiChildren)
      {
        if (child.GetType() == loadedForm)
        {
          child.Activate();
          return;
        }
      }

      Form frm = Activator.CreateInstance(loadedForm) as Form;
      frm.MdiParent = this;
      frm.Show();
    }

    /// <summary>
    /// Apply permission to main menu.
    /// </summary>
    private void ApplyMenuPermission()
    {
      Permission permission = GlobalCache.Permission;

      bbiDoiMatKhau.Enabled = permission.HasPermission(Permission.Functions.DoiMatKhau);

      bbiDsCapThe.Enabled = permission.HasPermission(Permission.Functions.XemDanhSachCapThe);
      bbiDsDaDuyet.Enabled = permission.HasPermission(Permission.Functions.XemDanhDachDaDuyet);
      bbiDsChuaDuyet.Enabled = permission.HasPermission(Permission.Functions.XemDanhDachChuaDuyet);
      bbiDuyetDsCapThe.Enabled = permission.HasPermission(Permission.Functions.DuyetDangKy);

      bbiDsBenhNhanDieuTri.Enabled = permission.HasPermission(Permission.Functions.XemDanhSachDieuTri);
      bbiDsDieuTriArv.Enabled = permission.HasPermission(Permission.Functions.XemDanhSachArv);
      bbiChamDutDieuTri.Enabled = permission.HasPermission(Permission.Functions.ChamDutDieuTri);
      bbiDkArv.Enabled = permission.HasPermission(Permission.Functions.DangKyDieuTriArv);
      bbiTiepNhanBenhNhan.Enabled = permission.HasPermission(Permission.Functions.ThayOpc);

      bbiDsBnChuyenDen.Enabled = permission.HasPermission(Permission.Functions.XemDanhSach);
      bbiDsBnChuyenDi.Enabled = permission.HasPermission(Permission.Functions.XemDanhSach);
      bbiDsBnMatHut.Enabled = permission.HasPermission(Permission.Functions.XemDanhSach);
      bbiDsBnTuVong.Enabled = permission.HasPermission(Permission.Functions.XemDanhSach);

      bbiLichSuChuyenBenh.Enabled = permission.HasPermission(Permission.Functions.XemLichSuChuyenBenh);

      bbiBaoCaoThang.Enabled = permission.HasPermission(Permission.Functions.XemBaoCaoThang);
    }

    #endregion

    #region Event Handlers

    #region Form Handlers

    private void FrmMain_Load(object sender, EventArgs e)
    {
      Text = MainFormUtils.GetTitle();
    }

    private void FrmMain_Shown(object sender, EventArgs e)
    {
      Login();
    }

    #endregion

    #region Menu Items

    #region TapTin

    private void bbiDoiMatKhau_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      using (FrmChangePassword frm = new FrmChangePassword { Owner = this })
      {
        frm.ShowDialog();
      }
    }

    private void bbiLogout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      Array.ForEach(MdiChildren, child => child.Close());
      
      //Only one place to remove Permission
      GlobalCache.Permission = null;
      //Get title (without username)
      Text = MainFormUtils.GetTitle();

      Login();
    }

    private void bbiThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      Application.Exit();
    }

    #endregion

    #region QuanLyDsCapThe

    private void bbiDsCapThe_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsCapThe));
    }

    private void bbiDsDaDuyet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsDaDuyet));
    }

    private void bbiDuyetDsCapThe_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsDuyetCapThe));
    }

    private void bbiDsChuaDuyet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsChuaDuyet));
    }

    #endregion

    #region QuanLyDsDieuTri

    private void bbiDsBenhNhanDieuTri_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsDieuTri));
    }

    private void bbiDsDieuTriArv_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsDieuTriArv));
    }

    private void bbiChamDutDieuTri_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      using (FrmChonBenhNhan frm = 
        new FrmChonBenhNhan (typeof(FrmChamDutDieuTri)) { Owner = this })
      {
        frm.ShowDialog();
      }
    }

    private void bbiDkArv_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      using (FrmChonBenhNhan frm =
        new FrmChonBenhNhan(typeof(FrmDkDieuTriArv)) { Owner = this })
      {
        frm.ShowDialog();
      }
    }

    private void bbiTiepNhanBenhNhan_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmTiepNhanBenhNhan));
    }

    #region DsBenhNhan

    private void bbiDsBnChuyenDen_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsBnChuyenDen));
    }

    private void bbiDsBnChuyenDi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsBnChuyenDi));
    }

    private void bbiDsBnMatHut_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsBnMatHut));
    }

    private void bbiDsBnTuVong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmDsBnTuVong));
    }

    #endregion

    #endregion

    #region ThongTinBenhNhan

    private void bbiLichSuChuyenBenh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      LoadForm(typeof(FrmLichSuChuyenBenh));
    }

    #endregion

    #region BaoCao

    private void bbiBaoCaoThang_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
      using (FrmChonBaoCaoThang frm = new FrmChonBaoCaoThang { Owner = this })
      {
        frm.ShowDialog();
      }
    }

    #endregion

    #endregion

    #endregion

    #region For Processing GUI Handlers

    private void xtraTabbedMdiManager_SelectedPageChanged(object sender, EventArgs e)
    {
      MainFormUtils.SetDefaultStatus();
    }

    #endregion
  }
}